package com.aabte.design.pattern.structural.proxy.staticproxy;

import com.aabte.design.pattern.structural.proxy.IOrderService;
import com.aabte.design.pattern.structural.proxy.Order;
import com.aabte.design.pattern.structural.proxy.OrderServiceImpl;
import com.aabte.design.pattern.structural.proxy.db.DataSourceContextHolder;

public class OrderServiceStaticProxy {

    private IOrderService orderService = new OrderServiceImpl();

    public int saveOrder(Order order) {

        beforeMethod(order);

        int result = orderService.saveOrder(order);

        afterMethod();

        return result;
    }

    private void beforeMethod(Order order){
        int userId = order.getUserId();
        int dbRouter = userId % 2;
        System.out.println("静态代理分配到[db"+dbRouter+"]处理数据");

        // 设置DataSource
        DataSourceContextHolder.setDBType("db"+String.valueOf(dbRouter));
        System.out.println("静态代理 before code");
    }

    private void afterMethod(){
        System.out.println("静态代理 after code");
    }

}
